package com.shangxia.lucky.lucky.writeHandler;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: monomer
 * @ClassName: MyWriteHandler
 * @Time: 2021/8/13
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
@Data
public class MyOverThreadExcelWriteHandler extends LongestMatchColumnWidthStyleStrategy implements CellWriteHandler, SheetWriteHandler {
    /**
     * 变色列
     */
    public static CellStyle CELLSTYLE;
    public org.apache.poi.ss.usermodel.Sheet Sheet;
    public List<List<String>> redLastList;

    public MyOverThreadExcelWriteHandler(List<List<String>> RED_LAST_LIST) {
        this.redLastList = RED_LAST_LIST;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        if (Sheet == null) {
            Sheet = cell.getSheet();
            //设置行宽
            Sheet.setDefaultColumnWidth(3);
        }
        setCellRule(cell);
    }

    private void setCellRule(Cell cell) {
        if (null == CELLSTYLE) {
            Workbook workbook = cell.getSheet().getWorkbook();
            CELLSTYLE = workbook.createCellStyle();
            //设置前景填充样式
            CELLSTYLE.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            CELLSTYLE.setFillForegroundColor(IndexedColors.RED.getIndex());
        }

        try {
            cell.setCellType(CellType.STRING);
            if (cell.getRowIndex() > 1 && redLastList.size() > (cell.getRowIndex() - 2)
                    && redLastList.get(cell.getRowIndex() - 2).contains(cell.getStringCellValue())) {
                cell.setCellStyle(CELLSTYLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.createFreezePane(0, 2, 1, 2);
    }
}
